//##include('../block/doT.js');
//##include('../block/swiper.js');
//##include('../block/initPhotoSwipeIndex.js');
//##include('../block/direction.js');


var $doc,$win,$header,$summary,$brandIndex,$filterContainer,$listTopArr,$filterList,$fsSummary,$otherSummary,$otherListUl;

var _view = {

    init: function() {
    }

};

var _event = {

    bind: function() {
        $doc.on('click', $summary, _event.toggleBrandList);
        $doc.on('click', $brandIndex, _event.initMarginH);
        $doc.on('click', '.brand-filter-index a', _event.anchorFn);//fashion-summary
        $doc.on('click', $otherSummary , _event.loadOtherBrand);

        $filterContainer.scroll(_event.fixedTopBar)

    },
    toggleBrandList: function() {
        var $this = $(this);
        $summary.removeClass('left-red-line');
        if($this.attr('data-flag') !== '1') {
            $summary.removeAttr('data-flag');
            $summary.parents().removeAttr('open');
            $summary.find('.u-icon-arr').removeClass('u-icon-arrU');

            $this.addClass('left-red-line');
            $this.attr('data-flag', '1');
            $this.find('.u-icon-arr').addClass('u-icon-arrU');
        }else{
            $this.removeAttr('data-flag');
            $this.find('.u-icon-arr').removeClass('u-icon-arrU');
        }
    },
    initMarginH: function(){
        var topH = $('header').offset().height + $('summary').offset().height + $('#brand-filter-index').offset().height;
        var _list = $filterList.find('section');

        if($.isEmptyObject($listTopArr)){
            $(_list).each(function(){
                var letterId = $(this).attr('id');
                $listTopArr[letterId] = $(this).offset().top - topH;
            });
        }

    },
    anchorFn : function(ev){
        var $this = $(this);
        var ev= ev || window.event;
        var id=$($this).attr('data-hash');

        $filterList.css({'margin-top':-$listTopArr[id.slice(1)]});
        ev.preventDefault();
    },

    loadOtherBrand:function(){
        var _this = $(this);
        var _urlHref = window.location.href;
        var type = _urlHref.substring(parseInt(_urlHref.lastIndexOf('/')+1)).split('_')[1];

        if(_this.next().children().length <= 0){
            $.ajax({
                type: 'GET',
                url: '/pedia/pedia_apis/otherbrand?type='+type+'&s=' + _this.attr('data-id'),
                dataType: 'json',
                success: function(data){
                    var temID = _this.next().attr('temId');
                    var tmpl = $("#"+temID).html();
                    var doTtmpl = doT.template(tmpl);
                    _this.next().html(doTtmpl(data));
                },
                error: function(){
                    console.log('Ajax error!');
                }
            })
        }
    },

    fixedTopBar:function(){
        //console.log($fsSummary.offset())
    }
};

var _data = {};

function init() {

    _view.init();
    _event.bind();

}

$(document).ready(function() {

    $doc = $(document);
    $win = $(window);
    $header = $('header');
    $summary = $('#filter-brand summary');
    $brandIndex = $('#brand-filter-index');
    $filterList = $('.filter-brand-list');
    $fsSummary = $('#fashion-summary');
    $otherSummary = $('.other-summary');
    $otherListUl = $('.other-list-ul');
    $filterContainer = $('.filter-container');

    $listTopArr = {};

    init();
});